/*
 * CppEvent1.h
 *
 *  Created on: 20-08-2011
 *      Author: cristian
 */

#ifndef CPPEVENT1_H_
#define CPPEVENT1_H_

#include <map>

#include "EventHandler1.cpp"


template<class ReturnT, class ParamT>
class CppEvent1 {
	typedef std::map<int, EventHandlerBase1<ReturnT, ParamT> *> HandlersMap;
	HandlersMap m_handlers;
	int m_count;

public:

	CppEvent1() :
			m_count(0) {
	}


	template<typename Listener> int attach(Listener* object , ReturnT (Listener::*member)(ParamT) )
	    {
	        ReturnT (Listener::*PtrMember)(ParamT);

	        m_handlers[m_count] = (new EventHandler1<Listener,
	                                ReturnT,ParamT>(object,member));
	        m_count++;
	        return m_count-1;
	    }

	    bool detach( int id)
	    {
	    	typename HandlersMap::iterator it = m_handlers.find(id);

	        if(it == m_handlers.end())
	            return false;

	        delete it->second;
	        m_handlers.erase(it);
	        return true;
	    }

	    ReturnT notify(ParamT param)
	    {
	    	typename HandlersMap::iterator it = m_handlers.begin();
	        for(; it != m_handlers.end(); it++)
	        {
	            it->second->notify(param);
	        }

	        return true;
	    }

};
#endif /* CPPEVENT1_H_ */
